﻿using System;
using System.Web;
using System.Data;
using SharedLibraries;
using SharedLibraries.Constants;
using EntityObject;
using DataAccessObject;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;

namespace ThangNM.Account
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                MsgUC1.setClearMsg();
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.INFO, "Trang web tạm thời chưa cho đăng ký tài khoản.<BR/>Vui lòng liên hệ <a href=\"mailto:admin@nguyenmanhthang.net\" title=\"admin@nguyenmanhthang.net\">admin@nguyenmanhthang.net</a> để được hỗ trợ.");
                return;
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        public DataSet Check(Int16 iType)
        {
            DataSet dsOutput = null;
            try
            {
                AccountEO _AccountEO = new AccountEO();
                _AccountEO.sUsername = txtsUsername.Text;
                _AccountEO.sPassword = Security.EnCrypt(txtsPassword.Text.Trim());
                _AccountEO.iPermission = iType;
                _AccountEO.bStatus = true;
                dsOutput = (new ShareDAO.UserDAO()).Login(_AccountEO);
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
            return dsOutput;
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            MsgUC1.setNotifications(ConstEx.FunctionSetMsg.INFO, MessagesEx.CHUC_NANG_NAY_DANG_TAM_KHOA);
            return;
            try
            {
                if (CheckInput())
                {
                    AccountEO _AccountEO = new AccountEO();
                    SEOEO _SEOEO = new SEOEO();
                    _AccountEO.sUsername = txtsUsername.Text;
                    _AccountEO.sPassword = Security.EnCrypt(txtsPassword.Text);
                    _AccountEO.sEmail = txtsEmail.Text;
                    _AccountEO.sFullName = txtsUsername.Text;
                    //_AccountEO.tDateOfBirth = DateTime.Now;
                    //_AccountEO.sPhoneNumber = "";
                    //_AccountEO.sLinkAvatar = "";
                    //_AccountEO.sSignature = "";
                    _AccountEO.iPermission = Account_iPermission_C.Anonymous;
                    //_AccountEO.sPermission = GetTextConstants.Account_iPermission_Default_GTC(_AccountEO.iPermission);
                    //_AccountEO.bStatus = Account_iStatus_C.Xem_Xet;
                    //Commons.setInfoSitemap(ref _SEOEO, TypeSiteMap.Account);
                    _SEOEO.sUrlRewrite = FileNameByFunction.Tac_Gia_ + RewriteUrl.ConvertToUnSign(_AccountEO.sUsername) + FileNameByFunction.HTML;
                    _SEOEO.bStatus = true;
                    //_AccountEO.PK_iAccountID = ShareDAO.UserDAO.Insert(_AccountEO,_SEOEO);
                    if (_AccountEO.PK_iAccountID > 0)
                    {
                        LoginEO _LoginEO = new LoginEO();
                        //Response.Cookies["secret"].Expires = DateTime.Now.AddDays(1);
                        _LoginEO.PK_iAccountID = _AccountEO.PK_iAccountID;
                        _LoginEO.sUsername = txtsUsername.Text;
                        _LoginEO.sIP = Request.ServerVariables["remote_addr"].ToString();
                        _LoginEO.tCookiesExpires = DateTime.Now.AddDays(1);
                        string CookiesValue = JsonEx.Object2Json(_LoginEO);
                        Session["secret"] = Security.Login.Encrypt(CookiesValue, Security.Base64Encode(Request.ServerVariables["remote_addr"]));
                        if (Request.QueryString[QueryString.return_url] == null)
                            Response.Redirect("~/Admin/ProfileUI.aspx");
                        else
                            Response.Redirect(Request.QueryString[QueryString.return_url].ToString());

                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Tạo tài khoản thành công.");
                    }
                    else
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Tạo tài khoản không thành công.");
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected bool CheckInput()
        {
            string strNull = "";
            if (string.IsNullOrEmpty(txtsEmail.Text) == true)
            {
                strNull += ", Địa chỉ email";
                txtsEmail.Focus();
            }
            else
            {
                if (!CommonsCheck.ValidateObject(txtsEmail.Text, 11))
                {
                    txtsEmail.Text = "";
                    txtsEmail.Focus();
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.Khong_Dung_Dinh_Dang_Email);
                    return false;
                }
                else
                {
                    if ((new ShareDAO.UserDAO()).CheckExists(txtsEmail.Text, 3))
                    {
                        txtsEmail.Text = "";
                        txtsEmail.Focus();
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.Email_Da_Duoc_Su_Dung);
                        return false;
                    }
                }
            }
            if (string.IsNullOrEmpty(txtsUsername.Text) == true)
            {
                strNull += ", Tên đăng nhập";
                txtsUsername.Focus();
            }
            else
            {
                if ((new ShareDAO.UserDAO()).CheckExists(txtsUsername.Text, 2))
                {
                    txtsUsername.Text = "";
                    txtsUsername.Focus();
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.Ten_Tai_Khoan_Da_Duoc_Su_Dung);
                    return false;
                }
            }
            if (string.IsNullOrEmpty(txtsPassword.Text) == true)
            {
                strNull += ", Mật khẩu";
                txtsPassword.Focus();
            }
            if (string.IsNullOrEmpty(txtsPassword1.Text) == true)
            {
                strNull += ", Nhập lại mật khẩu";
                txtsPassword1.Focus();
            }

            if (String.IsNullOrEmpty(strNull))
            {
                if (!string.IsNullOrEmpty(txtsPassword.Text) && !string.IsNullOrEmpty(txtsPassword1.Text) && !txtsPassword.Text.Equals(txtsPassword1.Text))
                {
                    txtsPassword.Focus();
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.Mat_Khau_Nhap_Lai_Khong_Trung_Khop);
                    return false;
                }
                if (!CommonsCheck.PasswordOk(txtsPassword.Text))
                {
                    txtsPassword.Focus();
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.Mat_Khau_Khong_Du_Manh);
                    return false;
                }

                if (!CaptchaUC1.CheckValidCaptcha())
                {
                    CaptchaUC1.ReCaptcha();
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.Captcha_Khong_Chinh_Xac);
                    return false;
                }
                if (chkAgree.Checked == false)
                {
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.Ban_Chua_Dong_Y_Voi_Dieu_Khoan_Cua_Chung_Toi);
                    return false;
                }
                return true;
            }
            else
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, MessagesEx.CAC_TRUONG_BAT_BUOC_PHAI_NHAP + strNull.Substring(2).Replace(":", ""));
                return false;
            }
        }
    }
}